package com.liu.manager.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.RolerAndPermission;
import com.liu.manager.entity.UserAndRoler;
import com.liu.manager.service.UserAndRolerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/userandroler")
public class UserAndRolerController {

    @Resource
    UserAndRolerService userAndRolerService;

    @PostMapping(value = "/addUserAndRoler",produces = {"application/json;charset=utf-8"})
    public String addUserAndRoler(@RequestBody UserAndRoler userAndRoler){
        ResponseResult responseResult = userAndRolerService.insertUserAndRoler(userAndRoler);
        return JSON.toJSONString(responseResult, JSONWriter.Feature.FieldBased,JSONWriter.Feature.WriteNulls);
    }

    @GetMapping(value = "/findByUserId",produces = {"application/json;charset=utf-8"})
    public String findByUserId(long uid){
        ResponseResult responseResult = userAndRolerService.findByUserId(uid);
        return JSON.toJSONString(responseResult, JSONWriter.Feature.FieldBased,JSONWriter.Feature.WriteNulls);
    }

    @GetMapping(value = "/findById",produces = {"application/json;charset=utf-8"})
    public String findById(long id){
        ResponseResult responseResult = userAndRolerService.findById(id);
        return JSON.toJSONString(responseResult, JSONWriter.Feature.FieldBased,JSONWriter.Feature.WriteNulls);
    }

    @GetMapping(value = "/findByRolerId",produces = {"application/json;charset=utf-8"})
    public String findByRolerId(long rid){
        ResponseResult responseResult = userAndRolerService.findByRolerId(rid);
        return JSON.toJSONString(responseResult, JSONWriter.Feature.FieldBased,JSONWriter.Feature.WriteNulls);
    }

    @PostMapping(value = "/removeById",produces = {"application/json;charset=utf-8"})
    public String removeById(long id){
        ResponseResult responseResult = userAndRolerService.removeById(id);
        return JSON.toJSONString(responseResult, JSONWriter.Feature.FieldBased,JSONWriter.Feature.WriteNulls);
    }
}
